package com.zhoug.share3.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.zhoug.share3.ClientType;
import com.zhoug.share3.R;
import com.zhoug.share3.ShareCallback;
import com.zhoug.share3.ShareClient;
import com.zhoug.share3.ShareClientImp;
import com.zhoug.share3.ShareParams;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 分享窗口
 *
 * @Author 35574
 * @Date 2021/10/14
 * @Description
 */
public class ShareDialog extends Dialog implements ShareAdapter.OnItemClickListener, ShareCallback {
    private Object tag;
    private int spanCount = 4;
    private ShareParams shareParams;
    private ShareClient shareClient;
    private ShareCallback shareCallback;

    public static final int CLIENT_Wechat = 1;//000001
    public static final int CLIENT_WechatMoments = 2;//000010
    public static final int CLIENT_QQ = 4;//000100
    public static final int CLIENT_QZone = 8;//001000
    public static final int CLIENT_SinaWeibo = 16;//010000
    public static final int CLIENT_Local = 32;//100000
    public static final int CLIENT_ALL = 63;//111111
    private int clientFlag = CLIENT_ALL;

    public ShareDialog(@NonNull Context context) {
        super(context, R.style.share_dialog_full);
        shareClient = new ShareClientImp(context);
        shareClient.setShareCallback(this);
    }


    public ShareDialog(@NonNull Context context, ShareParams shareParams) {
        super(context, R.style.share_dialog_full);
        this.shareParams = shareParams;
        shareClient = new ShareClientImp(context);
        shareClient.setShareCallback(this);
    }

    private List<ShareBean> initShareBeans() {
        List<ShareBean> shareBeans = new ArrayList<>();
        if ((clientFlag & CLIENT_Wechat) == CLIENT_Wechat) {
            if (shareParams != null) {
                if ((shareParams.getShareType() == ShareParams.SHARE_TYPE_IMAGE && ClientType.Wechat.isSupportImage())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_TEXT && ClientType.Wechat.isSupportText())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_HTML && ClientType.Wechat.isSupportHtml())
                ) {
                    shareBeans.add(new ShareBean("微信", R.drawable.share_icon_wechat, ClientType.Wechat));
                }
            } else {
                shareBeans.add(new ShareBean("微信", R.drawable.share_icon_wechat, ClientType.Wechat));
            }
        }
        if ((clientFlag & CLIENT_WechatMoments) == CLIENT_WechatMoments) {
            if (shareParams != null) {
                if ((shareParams.getShareType() == ShareParams.SHARE_TYPE_IMAGE && ClientType.WechatMoments.isSupportImage())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_TEXT && ClientType.WechatMoments.isSupportText())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_HTML && ClientType.WechatMoments.isSupportHtml())
                ) {
                    shareBeans.add(new ShareBean("朋友圈", R.drawable.share_icon_wechat_moment, ClientType.WechatMoments));
                }
            } else {
                shareBeans.add(new ShareBean("朋友圈", R.drawable.share_icon_wechat_moment, ClientType.WechatMoments));
            }
        }
        if ((clientFlag & CLIENT_QQ) == CLIENT_QQ) {
            if (shareParams != null) {
                if ((shareParams.getShareType() == ShareParams.SHARE_TYPE_IMAGE && ClientType.QQ.isSupportImage())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_TEXT && ClientType.QQ.isSupportText())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_HTML && ClientType.QQ.isSupportHtml())
                ) {
                    shareBeans.add(new ShareBean("QQ", R.drawable.share_icon_qq, ClientType.QQ));
                }
            } else {
                shareBeans.add(new ShareBean("QQ", R.drawable.share_icon_qq, ClientType.QQ));
            }
        }
        if ((clientFlag & CLIENT_QZone) == CLIENT_QZone) {
            if (shareParams != null) {
                if ((shareParams.getShareType() == ShareParams.SHARE_TYPE_IMAGE && ClientType.QZone.isSupportImage())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_TEXT && ClientType.QZone.isSupportText())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_HTML && ClientType.QZone.isSupportHtml())
                ) {
                    shareBeans.add(new ShareBean("QQ空间", R.drawable.share_icon_qqzone, ClientType.QZone));
                }
            } else {
                shareBeans.add(new ShareBean("QQ空间", R.drawable.share_icon_qqzone, ClientType.QZone));
            }
        }
        if ((clientFlag & CLIENT_SinaWeibo) == CLIENT_SinaWeibo) {
            if (shareParams != null) {
                if ((shareParams.getShareType() == ShareParams.SHARE_TYPE_IMAGE && ClientType.SinaWeibo.isSupportImage())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_TEXT && ClientType.SinaWeibo.isSupportText())
                        || (shareParams.getShareType() == ShareParams.SHARE_TYPE_HTML && ClientType.SinaWeibo.isSupportHtml())
                ) {
                    shareBeans.add(new ShareBean("新浪微博", R.drawable.share_icon_sinaweibo, ClientType.SinaWeibo));
                }
            } else {
                shareBeans.add(new ShareBean("新浪微博", R.drawable.share_icon_sinaweibo, ClientType.SinaWeibo));
            }
        }
        if ((clientFlag & CLIENT_Local) == CLIENT_Local) {
            shareBeans.add(new ShareBean("保存本地", R.drawable.share_icon_local, ClientType.Local));
        }
        return shareBeans;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//必须设置无标题 否则不会匹配内容
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.share_dialog, null);
        RecyclerView recyclerView = contentView.findViewById(R.id.share_recyclerView);
        setContentView(contentView);

        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), spanCount));
        ShareAdapter shareAdapter = new ShareAdapter(getContext());
        shareAdapter.setData(initShareBeans());
        shareAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(shareAdapter);


        _setAttributes();
        if (this.getWindow() != null) {
            this.getWindow().setWindowAnimations(R.style.share_dialog_anim);
        }

    }

    @Override
    public void onItemClick(ShareAdapter adapter, ShareBean item, int position) {
        dismiss();
        shareClient.share(getContext(), item.getClientType(), shareParams);
    }


    @Override
    public void onKeepLocalSuccess(String keepPath) {
        if (null != shareCallback) {
            shareCallback.onKeepLocalSuccess(keepPath);
        } else {
            toast(keepPath);
        }
    }

    @Override
    public void onShareError(Throwable error) {
        if (null != shareCallback) {
            shareCallback.onShareError(error);
        } else {
            if (error != null) {
                toast(error.getMessage());
            }
        }

    }

    public void setShareCallback(ShareCallback shareCallback) {
        this.shareCallback = shareCallback;
    }

    private void toast(String msg) {
        Toast.makeText(getContext(), msg != null ? msg : "", Toast.LENGTH_SHORT).show();
    }

    /**
     * 设置窗口的属性
     */
    private void _setAttributes() {
        Window window = this.getWindow();
        if (window == null) {
            return;
        }
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.gravity = Gravity.BOTTOM;
        window.setAttributes(attributes);
    }


    public Object getTag() {
        return tag;
    }

    public ShareDialog setTag(Object tag) {
        this.tag = tag;
        return this;
    }

    public ShareParams getShareParams() {
        return shareParams;
    }

    public ShareDialog setShareParams(ShareParams shareParams) {
        this.shareParams = shareParams;
        return this;
    }

    public ShareDialog setSpanCount(int spanCount) {
        this.spanCount = spanCount;
        return this;
    }

    /**
     * 设置分享的平台,会清除以前的平台设置
     *
     * @param clientFlag
     * @return
     */
    public ShareDialog setClientFlag(int clientFlag) {
        this.clientFlag = clientFlag;
        return this;
    }

    /**
     * 清除平台设置
     *
     * @return
     */
    public ShareDialog clearClientFlag() {
        this.clientFlag = 0;
        return this;
    }

    /**
     * 增加分享的平台
     *
     * @param clientFlag
     * @return
     */
    public ShareDialog addClientFlag(int clientFlag) {
        this.clientFlag |= clientFlag;
        return this;
    }


}
